/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.entity.entity.IChildEntityData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.Objects;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class ParentIdPropGenerator extends ClassPropertyGenerator {

  private static final String propName = "ParentID";
  private MediateType parentInterface;
  private String parentIdPropName;

  public ParentIdPropGenerator(MediateType parentInterface, IGspCommonObject node) {
    this(parentInterface, getParentIdLabel(node));
  }
  public ParentIdPropGenerator(MediateType parentInterface, String parentIdPropName) {
    Objects.requireNonNull(parentInterface);
    Objects.requireNonNull(parentIdPropName);

    this.parentInterface = parentInterface;
    this.parentIdPropName = parentIdPropName;
  }

  private static String getParentIdLabel(IGspCommonObject node){
    if(node.getKeys()== null || node.getKeys().isEmpty()) {
      return null;
    }
    IGspCommonElement element = node.findElement(node.getKeys().get(0).getSourceElement());
    return element == null ? null : element.getLabelID();
  }

  public static boolean NeedGenerate(IGspCommonObject node) {
    return !propName.equals(getParentIdLabel(node));
  }

  @Override
  protected void doInitialize() {
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected Block buildGetMethodBody() {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();

    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName(String.format("get%1$s", parentIdPropName)));
    returnStatement.setExpression(methodInvocation);
    block.statements().add(returnStatement);
    return block;
  }

  @Override
  protected Block buildSetMethodBody() {
    Block block = ast.newBlock();
    Assignment assignment = ast.newAssignment();
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName(String.format("set%1$s", parentIdPropName)));
    methodInvocation.arguments().add(ast.newSimpleName("value"));
//		assignment.setLeftHandSide(methodInvocation);
//
//		assignment.setOperator(Assignment.Operator.ASSIGN);
//
//		assignment.setRightHandSide(ast.newSimpleName("value"));

    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
    block.statements().add(expressionStatement);
    return block;
  }

  @Override
  protected String getPropertyName() {
    return propName;
  }

  @Override
  protected TypeInfo getPropertyType() {
    return new TypeInfo(String.class.getTypeName());
  }

  @Override
  protected String getFieldName() {
    return null;
  }
}
